package com.szlanyou.tcp_server_demo;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.camnter.easyrecyclerview.widget.EasyRecyclerView;
import com.szlanyou.tcp_server_demo.adapter.ClientAdapter;
import com.szlanyou.tcp_server_demo.adapter.ClientItemModel;
import com.szlanyou.tcpservermaster.TcpSocketServer;
import com.szlanyou.tcpservermaster.TcpClientItem;
import com.szlanyou.tcpservermaster.util.NetUtil;

import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView tvState;
    private TcpSocketServer controlServer;
    private TextView btnServerControl;
    private ClientAdapter clientAdapter;
    private ClientPool clientPool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupClientPool();
        initView();
    }

    private void setupClientPool() {
        clientPool = new ClientPool();
        clientPool.setOnPoolChangedListener(new ClientPool.OnPoolChangedListener() {
            @Override
            public void onPoolChange(List<ClientItemModel> list) {
                clientAdapter.set(list);
            }
        });
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        tvState = findViewById(R.id.tv_server_state);
        btnServerControl = findViewById(R.id.btn_server_control);
        initToolBar();
        initClientList();
    }

    private void initClientList() {
        EasyRecyclerView rcvClient = findViewById(R.id.rcv_client);
        clientAdapter = new ClientAdapter();
        rcvClient.setAdapter(clientAdapter);
    }

    private void initToolBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.setTitle(R.string.app_name);
            setSupportActionBar(toolbar);
        }
    }

    private void initServer() {
        controlServer = new TcpSocketServer(9999);
        final String ip = NetUtil.getIp(MainActivity.this);
        controlServer.listenServer(new TcpSocketServer.OnServerStatusListener() {
            @Override
            public void onServerStarted() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvState.setText("服务" + ip + "已经开启");
                        btnServerControl.setText("关闭服务");
                    }
                });
            }

            @Override
            public void onServerClosed() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvState.setText("服务" + ip + "已经关闭");
                        btnServerControl.setText("开启服务");
                    }
                });
            }
        });

        controlServer.listenClient(new TcpClientItem.OnClientStatus() {
            @Override
            public void onJoined(final TcpClientItem client) {
                clientPool.join(client);
            }

            @Override
            public void onDisconnected(final TcpClientItem client) {
                clientPool.leave(client);
            }

            @Override
            public void onDisconnect(final TcpClientItem client, Throwable e) {
                clientPool.leave(client);
            }

            @Override
            public void onError(final TcpClientItem client, final Throwable e) {

            }

            @Override
            public void onReceive(TcpClientItem client, InputStream in) {
                clientPool.get(client.getAddress()).protocolHandlerSingleton.get().handle(in);
            }

            @Override
            public void onSendMsgFailure(TcpClientItem client, Throwable e) {

            }
        });



        controlServer.enableServer();
    }

    public void startOrOpenServer(View view) {
        if(null ==  controlServer){
           initServer();
        }else{
           if(controlServer.isRun() == false){
               controlServer.enableServer();
               btnServerControl.setText("关闭服务");
           }else{
               controlServer.closeServer();
               btnServerControl.setText("开启服务");
           }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clientPool.release();
        controlServer.release();
    }
}